package rabbit.topic;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConfirmListener;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author: admin
 * @Description:
 * @Date: 2018/11/7 16:18
 */

public class TestTxMq {
    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("192.168.10.66");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("root");
        connectionFactory.setPassword("root");
        connectionFactory.setVirtualHost("OuterHost");
        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();
        //channel.exchangeDeclare("testexchange", BuiltinExchangeType.TOPIC, true);
        channel.txSelect();
        for (int i = 0; i < 10; i++) {
            try {
                channel.basicPublish("testexchange", "testtopic", null, "{\"name\":\"sjw\"}".getBytes("UTF-8"));
                if(i%2==0) throw new Exception();
                //channel.close();
                channel.txCommit();
            }catch (Exception e){
                channel.txRollback();
            }
        }
        System.out.println("end");
    }
}
